import { message } from 'antd';
import api from '../../api';

import config from '@/src/utils/config';
import apiCreator from '@/src/utils/apiCreator';
import uuid from 'es6-uuid';

const apiFun = apiCreator();
export default {
  namespace: 'CollectingMonitoringM', //汇总监控
  state: {},
  effects: {
    //查询产品列表
    *queryProuctList({ payload, callback }, { call, put, select }) {
      const res = yield call(api.CollectingMonitoring.queryProuctList, payload);
      if (res && res.code == 0) {
        callback(res.data);
      } else {
        callback();
        message.error(res ? res.msg || '查询产品列表失败!' : '查询产品列表失败!');
      }
    },
    //新建/编辑模拟组合
    *addEditPortfolio({ payload, callback }, { call, put, select }) {
      const res = yield call(api.CollectingMonitoring.addEditPortfolio, payload);
      if (res && res.code == 0) {
        callback('success');
        message.success('提交成功!');
      } else {
        callback();
        message.error(res ? res.msg || '提交失败!' : '提交失败!');
      }
    },
    //删除某个组合
    *deleteById({ payload, callback }, { call, put, select }) {
      const res = yield call(api.CollectingMonitoring.deleteById, payload);
      if (res && res.code == 0) {
        callback();
        message.success('删除成功!');
      } else {
        message.error(res ? res.msg || '删除失败!' : '删除失败!');
      }
    },

    //获取某模拟组合详情
    *queryDetailById({ payload, callback }, { call, put, select }) {
      const res = yield call(api.CollectingMonitoring.queryDetailById, payload);
      if (res && res.code == 0) {
        res.data &&
          res.data?.items.map((item) => {
            item.id = uuid(32);
          });
        callback(res.data);
      } else {
        callback();
        message.error(res ? res.msg || '查询组合详情失败!' : '查询组合详情失败!');
      }
    },
    //模板上传
    *uploadFile({ payload, callback }, { call, put, select }) {
      // const res = yield call(api.CollectingMonitoring.uploadFile, payload);
      const formData = new FormData();

      formData.append('file', payload.file);

      //这段到时可放到model和api文件中
      apiFun
        .uploadFile(`${config.interfaces.type5}portfolio-server/simulationProduct/upload`, {}, formData)
        .then((res) => {
          if (res && res.code == 0) {
            res.importVo?.dataSource.map((item) => {
              item.id = uuid(32);
              item.weight = item.weight !== null ? item.weight * 1 : undefined;
            });
            res.importVo?.errorDataSource.map((item) => {
              item.id = uuid(32);
            });
            callback(res.importVo);
          } else {
            callback();
            message.error(res ? res.msg || '上传失败!' : '上传失败!');
          }
        })
        .catch((e) => {
          console.log(e, 'error');
        });
    },
    //查询行业
    *getIndustry({ payload, callback }, { call, put }) {
      const res = yield call(api.poolApproval.getIndustryAndRole, payload);
      if (res) {
        callback(res);
      } else {
        message.error('获取行业失败');
      }
    },
  },

  reducers: {
    setState(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
